
/**
 * AppleIIGo
 * SmartPort class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#ifndef SMARTPORT
#define SMARTPORT_H

#include "bitMacros.h"
#include "List.h"
#include "DiskPort.h"
#include "SmartPortUnit.h"

#define SMARTPORT_UNITNUM				128

#define SMARTPORTBUFFER_BYTENUM			0x400

class SmartPort : public DiskPort {
public:
	SmartPort();
	~SmartPort();
	
	void reset();
	
	void connectDrive(class SmartPortUnit * drive);
	
	void setPhaseRegister(UINT32 value);
	void setDriveEnabled(bool value);
	
	bool senseInput();
	
	void updateWriteBitNum(UINT32 * value);
	void sendBit(bool value);
	void updateReadBitNum(UINT32 * value);
	bool receiveBit();
	void discardBits(UINT32 bitNum);
	
protected:
	List * driveList;
	
	SmartPortUnit * unit[SMARTPORT_UNITNUM];
	
	bool isDriveEnabled;
	bool isPhase0;
	
	bool isReq;
	bool isAck;
	bool isReply;
	bool isSending;
	bool isReceiving;
	
	UINT8 shiftRegister;
	UINT32 shiftRegisterBitNum;
	
	bool isPacketData;
	UINT8 packetBuffer[SMARTPORTBUFFER_BYTENUM];
	UINT32 packetBufferIndex;
	UINT32 packetBufferByteNum;
	
	struct SmartPortPacket packet;
	
	void updateReq();
	
	void writePacketSync();
	void writePacketValue(UINT8 value);
	UINT8 readPacketValue();
	void writePacketGroup(UINT32 byteNum);
	void readPacketGroup(UINT32 byteNum);
	
	void writePacket();
	void readPacket();
	
	UINT32 initUnit();
};

#endif
